<?php
namespace Views;
/**
 * File holds class for page. Here menus and translations are loaded,
 * messages are stored and general view is put together
 *
 * PHP version 5.3.5
 *
 * @category   CM
 * @package    Views
 * @subpackage -
 * @author     markus karileet <markuskarileet@hotmail.com>
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL
 * @link       -
 */
class Page {
	const CONTENT_ONLY = 1;
	
	/**
	 * Array of translations
	 * @var array
	 */
	protected $_translation;
	/**
	 * Smarty template engine object
	 * @var \Smarty
	 */
	protected $_smarty;
	/**
	 * Currently logged in user TODO
	 * @var unknown_type
	 */
	protected $_loggedInUser;
	/**
	 * Array of messages to display to user 
	 * array('error|warning|success' => 'message string')
	 * @var array
	 */
	protected $_messages;
	/**
	 * Variable holds page content
	 * @var var
	 */
	protected $_content;
	
	/**
	 * Flag whether not to load menus
	 * @var int
	 */
	protected $_onlyContent;
	
	public function setUp($language, $onlyContent) {
		$this->_onlyContent = $onlyContent;
		$this->_loadLanguage($language);
		$this->_loadSmarty();
		$this->_smarty->assign('lang', $this->_translation);
		$this->_loadVerticalMenu();
		$this->_loadFooter();
	}
	
	/**
	 * Method shows main page
	 */
	public function viewWelcomePage() {
		$this->_content = $this->_smarty->fetch('welcome.tpl');
		$this->display();
	}
	
	/**
	 * Method populates vertical menu variable
	 */
	protected function _loadVerticalMenu() {
		if ($this->_onlyContent != self::CONTENT_ONLY) {
			$this->_smarty->assign('page', 'bestCompanies');
			$typeService = new \Services\Type();
			$this->_smarty->assign('items', $typeService->getAllTypes());
			$html = $this->_smarty->fetch('verticalNav.tpl');
			$this->_smarty->assign('vNav', $html);
		}
	}
	
	/**
	 * Method populates footer element
	 */
	protected function _loadFooter() {
		$html = $this->_smarty->fetch('footer.tpl');
		$this->_smarty->assign('footer', $html);
	}
	
	/**
	 * Method loads translation file
	 * @param var $language Language identifier
	 */
	private function _loadLanguage($language) {
		switch ($language) {
			case \Enum\Language::Eng:
				include 'i18n/eng.php';
				break;
			case \Enum\Language::Est:
				include 'i18n/est.php';
				break;
			default:
				include 'i18n/est.php';
				break;
		}
		$this->_translation = $lang;		
	}
	
	/**
	 * Method loads smarty object into class attribute
	 */
	private function _loadSmarty() {
		global $smarty;
		$this->_smarty = $smarty;
	}
	
	/**
	 * Method assigns messages to template and then displays general template
	 */
	public function display() {
		$this->_smarty->assign('messages', $this->_messages);
		$this->_smarty->assign('content', $this->_content);
		$this->_smarty->display('index.tpl');
	}
	
	/**
	 * Method allows passing messages to another page views
	 * @param array $message Message to pass
	 */
	public function setMessage($message) {
		$this->_messages = $message;
	}
}